package com.tiancheng.trade.settlement.controller.admin;

import com.tiancheng.trade.commom.core.data.Result;
import com.tiancheng.trade.settlement.bo.ServiceDetailCountExportBO;
import com.tiancheng.trade.settlement.bo.ServiceDetailCountRequestBO;
import com.tiancheng.trade.settlement.service.order.ISubOrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.util.regex.Pattern;

/**
 * @author kellen
 * @date 2021/5/19
 */
@Slf4j
@RestController
@RequestMapping("/admin/settle/subOrder")
public class SubOrderAdminController {
    @Autowired
    private ISubOrderService subOrderService;

    private static final String DAY_REGEX = "^(19|([2-9]\\d))\\d\\d(-|/)(0[1-9]|1[0-2])(-|/)([0-3][0-9])$";
    private static final String MONTH_REGEX = "^(19|([2-9]\\d))\\d\\d(-|/)(0[1-9]|1[0-2])$";
    private static final String YEAR_REGEX = "^(19|([2-9]\\d))\\d\\d$";

    /**
     * 订单数据统计汇总查询
     * 返回金额单位：元
     *
     * @param yearMonth {@link String} 参数格式：yyyy或yyyy-MM或yyyy/MM
     * @return {@link Result}
     */
    @GetMapping("/serviceAmountCount")
    public Result<?> serviceAmountCount(String yearMonth) {
        if (StringUtils.isBlank(yearMonth)) {
            return Result.fail("请求参数不正确");
        }
        int year;
        String month = null;
        if (Pattern.compile(MONTH_REGEX).matcher(yearMonth).find()) {
            year = Integer.parseInt(yearMonth.substring(0, 4));
            month = yearMonth.substring(5);
        } else if (Pattern.compile(YEAR_REGEX).matcher(yearMonth).find()) {
            year = Integer.parseInt(yearMonth);
        } else {
            return Result.fail("请求参数不正确");
        }
        return Result.success(subOrderService.serviceAmountCount(year, month));
    }

    /**
     * 订单数据明细统计查询-分页
     * 天周期和年周期二选一传一个
     *
     * @param requestBO {@link ServiceDetailCountRequestBO}
     * @return {@link Result}
     */
    @GetMapping("/serviceDetailCountPage")
    @Validated
    public Result<?> serviceDetailCountPage(ServiceDetailCountRequestBO requestBO) {
        Pattern pattern = Pattern.compile(DAY_REGEX);
        boolean isDayCycle = requestBO.getDayCycleStart() != null && requestBO.getDayCycleEnd() != null
                && pattern.matcher(requestBO.getDayCycleStart()).find() && pattern.matcher(requestBO.getDayCycleEnd()).find();
        boolean isYearCycle = null != requestBO.getYear();
        if ((isDayCycle && isYearCycle) || (!isDayCycle && !isYearCycle)) {
            return Result.fail("请求参数不正确");
        }
        if (isDayCycle) {
            requestBO.setDayCycleStart(requestBO.getDayCycleStart().replace("/", "-") + " 00:00:00.000");
            requestBO.setDayCycleEnd(requestBO.getDayCycleEnd().replace("/", "-") + " 23:59:59.999");
        }
        return Result.success(subOrderService.serviceDetailCountPage(requestBO));
    }

    /**
     * 订单数据明细统计导出
     *
     * @param exportBO {@link ServiceDetailCountExportBO}
     * @param request  {@link HttpServletRequest}
     * @param response {@link HttpServletResponse}
     */
    @GetMapping("/serviceDetailCountExport")
    @Validated
    public void serviceDetailCountExport(ServiceDetailCountExportBO exportBO, HttpServletRequest request,
                                         HttpServletResponse response) throws Exception {
        Pattern pattern = Pattern.compile(DAY_REGEX);
        boolean isDayCycle = exportBO.getDayCycleStart() != null && exportBO.getDayCycleEnd() != null
                && pattern.matcher(exportBO.getDayCycleStart()).find() && pattern.matcher(exportBO.getDayCycleEnd()).find();
        boolean isYearCycle = null != exportBO.getYear();
        if ((isDayCycle && isYearCycle) || (!isDayCycle && !isYearCycle)) {
            log.error("订单数据明细统计导出: 请求参数不正确");
            return;
        }
        if (isDayCycle) {
            exportBO.setDayCycleStart(exportBO.getDayCycleStart().replace("/", "-") + " 00:00:00.000");
            exportBO.setDayCycleEnd(exportBO.getDayCycleEnd().replace("/", "-") + " 23:59:59.999");
        }
        subOrderService.serviceDetailCountExport(exportBO, request, response);
    }
}
